package com.wxzz.elearing.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.user.dto.ToolFormDto;
import com.wxzz.elearing.user.entity.*;
import com.wxzz.elearing.user.mapper.SubToolFormMapper;
import com.wxzz.elearing.user.mapper.ToolFormMapper;
import com.wxzz.elearing.user.service.IToolFormService;
import com.wxzz.elearing.user.vo.AdminWorkGuidelineVo;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2021-05-18
 */
@Service
public class ToolFormServiceImpl extends ServiceImpl<ToolFormMapper, ToolForm> implements IToolFormService {

    @Resource
    private ToolFormMapper toolFormMapper;
    @Resource
    private SubToolFormMapper subToolFormMapper;
    public ReturnVO addToolForm(ToolForm toolForm){
        if(toolForm.getId()==null){
             toolFormMapper.insert(toolForm);
            return new ReturnVO("工具表单添加成功",null);
        }
        toolFormMapper.updateById(toolForm);
        return new ReturnVO("工具表单修改成功",null);
    }

    public ReturnVO<JgPageVo<List<ToolForm>>> toolFormList(ToolFormDto toolFormDto){
        Page<Object> objects = PageHelper.startPage(toolFormDto.getCurPage(), toolFormDto.getMaxPage());
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(toolFormDto.getToolFormName())){
            queryWrapper.like("tool_form_name",toolFormDto.getToolFormName());
        }
        queryWrapper.eq("enterprise_id",toolFormDto.getEnterpriseId());
        queryWrapper.orderByDesc("create_time");
        List<ToolForm> list = toolFormMapper.selectList(queryWrapper);
        list.forEach((model)->{
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("parent_id",model.getId());
            Integer count = subToolFormMapper.selectCount(queryWrapper1);
            model.setSubCount(count);
        });
        return new ReturnVO("工具表单列表查询成功！",new JgPageVo(new PageInfo<>(list)));
    }

    public ReturnVO deleteToolForm(ToolForm toolForm){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",toolForm.getId());
        Integer integer = subToolFormMapper.selectCount(queryWrapper);
        if(integer!=0){
            throw new ServiceException("该工具表单有二级工具表单,请先删除二级工具表单后继续操作！");
        }
        int i = toolFormMapper.deleteById(toolForm.getId());
        return new ReturnVO("删除工具表单成功",null);
    }
}


